<template>
  <div class="employees">
    <el-card>
      <PageTools>
        <template #right>
          <el-button
            v-if="userInfo.roles.points.includes('employees_importButton')"
            type="primary"
            @click="gUpload"
          >导入</el-button>
          <el-button type="primary" @click="addClick">+新增员工</el-button>
        </template>
      </PageTools>
    </el-card>

    <el-card style="margin-top: 20px">
      <el-table :data="list">
        <el-table-column label="序号" width="50">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              v-imgError="defaultImg"
              :src="row.staffPhoto"
              class="avatar"
              alt=""
              @click="imgClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column
          label="聘用形势"
          prop="formOfEmployment"
          width="80"
          :formatter="formatter"
        />
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column
          label="入职时间"
          prop="timeOfEntry"
          :formatter="formattertimeOfEntry"
        />

        <el-table-column label="操作" width="300px" fixed="right">
          <template v-slot="{ row }">
            <el-button type="text" @click="look(row)">查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text" @click="roleClick(row)">角色</el-button>
            <el-button type="text" @click="del(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <div class="page">
      <el-pagination
        :current-page="page.page"
        :page-sizes="[3, 5, 10, 20, 30]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加组件 -->
    <add ref="add" @getData="getData" />
    <!-- 头像二维码弹框 -->
    <el-dialog title="查看二维码" :visible.sync="showQrImg">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>

    <!-- 角色组件 -->
    <role ref="role" v-model="showRole" />
  </div>
</template>

<script>
import mixinJs from '@/mixin/index'
import add from './components/add.vue'
import role from './components/role.vue'
import { sysUser, sysUserDelete } from '@/api/employees'
import employees from '@/api/constant/employees'
import dayjs from 'dayjs'
import Qrcode from 'qrcode'
import { mapState } from 'vuex'
export default {
  components: {
    add,
    role
  },
  mixins: [mixinJs],
  data() {
    return {
      showRole: false,
      showQrImg: false,
      defaultImg: require('@/assets/common/head.jpg'),
      list: [],
      page: {
        page: 1, // 	是	1	页码
        size: 10, // 	是	10	每页条数:1, //
        total: 10
      }
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.getData()
  },
  methods: {
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    async getData() {
      const res = await sysUser(this.page)
      this.page.total = res.data.total
      // 格式化聘用形势数据
      // res.data.rows.forEach((item, index, arr) => {
      // const temp = employees.hireType.find((item) => {
      //   return item.id === arr.formOfEmployment
      // })
      // return temp ? temp.value : '-'
      // })
      // 存储员工信息
      this.list = res.data.rows
    },
    formatter(row, column, cellValue, index) {
      const temp = employees.hireType.find((item) => {
        return item.id === +cellValue
      })
      return temp ? temp.value : '-'
    },
    // 转换时间格式
    formattertimeOfEntry(row, column, cellValue, index) {
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    // 新增员工按钮
    addClick() {
      this.$refs.add.dialogVisible = true
    },
    // 删除员工按钮
    del(row) {
      // 询问框
      this.$confirm('请问你要删除该员工嘛', '提示').then(async() => {
        await sysUserDelete(row.id)
        // 提示信息
        this.$message.success('删除成功')
        // 回到第一页
        this.handleCurrentChange(1)
      })
    },
    // 导入按钮
    gUpload() {
      this.$router.push('/employees/upload?name=' + encodeURIComponent('员工'))
    },
    // 查看按钮
    look(row) {
      this.$router.push('/employees/detail/' + row.id)
    },
    // 查看头像二维码
    imgClick(imgUrl) {
      if (!imgUrl) return
      this.showQrImg = true
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, imgUrl, { width: 400, height: 400 })
      })
    },
    // 角色
    roleClick(row) {
      this.showRole = true
      this.$refs.role.getUserInfo(row.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 20px;
  .page {
    text-align: right;
  }
  .avatar {
    width: 30px;
    height: 30px;
    object-fit: contain;
  }
}
</style>
